package y24.m5.music.sound;

import javax.sound.sampled.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class ConvertToWav {
    public static void main1(String[] args) {
        try {
            // 输入音乐文件路径和输出wav文件路径
            String inputFilePath = "飞天.mp3";
            String outputFilePath = "music.wav";

            // 调用FFmpeg命令行执行音频格式转换
            String command = "ffmpeg -i " + inputFilePath + " -acodec pcm_s16le -ar 44100 -ac 2 " + outputFilePath;
            Process process = Runtime.getRuntime().exec(command);

            // 获取命令行输出信息
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

            process.waitFor();
            System.out.println("音乐文件转换完成。");

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        try {
            // 输入MP3文件路径和输出WAV文件路径
            String mp3FilePath = "飞天.mp3";
            String wavFilePath = "output.wav";

            AudioInputStream mp3Stream = AudioSystem.getAudioInputStream(new File(mp3FilePath));
            AudioFormat format = mp3Stream.getFormat();

            AudioInputStream convertedStream = AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED, mp3Stream);
            AudioSystem.write(convertedStream, AudioFileFormat.Type.WAVE, new File(wavFilePath));

            System.out.println("MP3文件转换为WAV格式完成。");

        } catch (UnsupportedAudioFileException | IOException e) {
            e.printStackTrace();
        }
    }
}
